<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>模拟网上银行系统 - 登录</title>
    <style>
        /* 全局样式重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background-color: #e6f7ff;
            font-family: "微软雅黑", sans-serif;
        }

        .login-container {
            width: 500px;
            margin: 100px auto;
            background-color: rgba(255, 255, 255, 0.9);
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        .login-container h1 {
            margin-bottom: 20px;
            color: #333;
        }

        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #666;
        }

        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 14px;
        }

        .btn-login {
            width: 100%;
            padding: 10px;
            background-color: #1890ff;
            border: none;
            color: #fff;
            font-size: 16px;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .btn-login:hover {
            background-color: #0078d7;
        }

        .extra-links {
            margin-top: 15px;
        }

        .extra-links a {
            text-decoration: none;
            color: #009688;
            margin: 0 10px;
            font-size: 14px;
        }

        .extra-links a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>

<div class="login-container">
    <h1>用户登录</h1>
    <form id="loginForm">
        <div class="form-group">
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" required />
        </div>
        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required />
        </div>
        <button type="submit" class="btn-login">登录</button>
    </form>

    <div class="extra-links">
        <a href="#">忘记密码？</a>
        <a href="#">注册新账户</a>
    </div>
</div>

<script>
    document.getElementById('loginForm').addEventListener('submit', async function (e) {
        e.preventDefault();

        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value.trim();

        if (!username || !password) {
            alert("请输入用户名和密码");
            return;
        }

        try {
            const response = await fetch('/user/login', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ username, password })
            });


            const result = await response.json();

            if (result.code === 2000 && result.data) {
                window.location.href = result.data; // 跳转到对应页面
            } else {
                alert(result.msg || '登录失败，请检查用户名或密码');
            }
        } catch (error) {
            console.error(error);
            alert("网络请求失败，请稍后再试");
        }
    });
</script>

</body>
</html>
